package cn.com.surker.material.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.material.dao.MainMaterialContractDao;
import cn.com.surker.material.dao.MainMaterialContractPriceDao;
import cn.com.surker.material.dao.MainSupplierDao;
import cn.com.surker.material.entity.MainMaterialContract;
import cn.com.surker.material.entity.MainMaterialContractPrice;
import cn.com.surker.material.entity.MainSupplier;
import cn.com.surker.material.service.MainMaterialContractService;
import cn.com.surker.oa.dao.OaMaterialContractDao;
import cn.com.surker.oa.entity.OaMaterialContract;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lucky
 * @create 2024年07月07 12:00
 * @desc
 */
@Slf4j
@Service
public class MainMaterialContractServiceImpl implements MainMaterialContractService {

    @Resource
    private MainMaterialContractPriceDao mainMaterialContractPriceDao;
    @Resource
    private MainMaterialContractDao mainMaterialContractDao;
    @Resource
    private MainSupplierDao mainSupplierDao;
    @Resource
    private OaMaterialContractDao materialContractDao;


    public List<MainSupplier> findSupplier() {
        QueryWrapper<MainSupplier> query = new QueryWrapper<>();
        query.eq("status", 0);
        List<MainSupplier> list = mainSupplierDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 查询合同价格信息
     * @param contractId
     * @param type
     * @return
     */
    public List<MainMaterialContractPrice> findContractPrice(Long contractId, Integer type, Integer status) {
        if (StringUtils.isNull(contractId))
            return null;
        if (StringUtils.isNull(status))
            status = 0;
        QueryWrapper<MainMaterialContractPrice> query = new QueryWrapper<>();
        query.eq("status", status);
        query.eq("contract_id", contractId);
        if (!StringUtils.isNull(type))
            query.eq("type", type);
        query.orderByDesc("take_time");
        List<MainMaterialContractPrice> list= mainMaterialContractPriceDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 分页查询材料信息
     * @param searchVal 检索条件
     * @param status 是否启用
     * @param type 材料大类枚举 SysMaterialTypeEnum
     * @param offset
     * @param limit
     * @return
     */
    public IPage findPage(String searchVal, Integer status, Integer type, Long orgId, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<OaMaterialContract> query = new QueryWrapper<>();
        if (StringUtils.isNull(status))
            status = 0;
        if (!StringUtils.isNull(status))
            query.eq("status", status);
        if (!StringUtils.isNull(type))
            query.eq("type", type);
        if (!StringUtils.isNull(searchVal))
            query.and(orderWrapper -> orderWrapper.like("name", searchVal).or()
                    .like("code", searchVal).or().like("link_man", searchVal)
                    .or().like("remark", searchVal));
        query.orderByDesc("id");

        IPage<OaMaterialContract> iPage = materialContractDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }


    /**
     * 修改状态信息
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Long id, Integer status) {
        if (StringUtils.isAllParamNull(id, status))
            return Code.PARAM_EMPTY.getCode();

        MainMaterialContract mainMaterial = mainMaterialContractDao.selectById(id);
        if (mainMaterial == null)
            return Code.ERROR_DATA.getCode();
        mainMaterial.setStatus(status);
        Integer resultCode = mainMaterialContractDao.updateById(mainMaterial);
        if (resultCode > 0)
            return 0;
        return -1;
    }
}
